package com.bltec.emergency.controller.resController;

import com.alibaba.fastjson.JSON;
import com.bltec.emergency.common.Constant;
import com.bltec.emergency.common.ResponseJson;
import com.bltec.emergency.model.ResTeam;
import com.bltec.emergency.service.resService.ResTeamService;
import com.bltec.emergency.service.mgrService.UserService;
import com.bltec.emergency.utils.DateUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/resTeam")
@RequiresPermissions("project:res")
public class ResTeamController {
    @Resource
    private ResTeamService resTeamService;
    @Resource
    private UserService userService;
    /**
     * 获得所有
     */
    @RequestMapping("getAllResTeam")
    public ResponseJson getResTeam() {
        try {
            List<ResTeam> resTeam = resTeamService.getAll();
            return new ResponseJson(Constant.OK, JSON.toJSON(resTeam));
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseJson(Constant.FAIL, e.getMessage());
        }
    }

    /**
     * 添加
     */
    @RequiresPermissions("res:insert")
    @RequestMapping("addResTeam")
    public ResponseJson addResTeam(String name, Long departmentId, Long type, String gpsX, String gpsY, String eventType, Long strength, String category, String mainEquipment, String phone, Boolean isFullTime) {
        try {
            ResTeam resTeam = new ResTeam();
            resTeam.setName(name);
            resTeam.setDepartmentId(departmentId);
            resTeam.setType(type);
            resTeam.setGpsX(new BigDecimal(gpsX));
            resTeam.setGpsY(new BigDecimal(gpsY));
            resTeam.setEventType(eventType);
            resTeam.setStrength(strength);
            resTeam.setCategory(category);
            resTeam.setMainEquipment(mainEquipment);
            resTeam.setPhone(phone);
            resTeam.setIsFullTime(isFullTime);
            resTeam.setEnable(true);
            resTeam.setCreateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            resTeam.setCreateTime(DateUtil.dateNow());
            resTeamService.add(resTeam);
            return new ResponseJson(Constant.OK, "创建用户成功");
        } catch (Exception e) {
            return new ResponseJson(Constant.FAIL, "创建用户失败" + e.getMessage());
        }
    }

    /**
     * 修改
     */
    @RequiresPermissions("res:update")
    @RequestMapping("updateResTeam")
    public ResponseJson updateResTeam(Long id,String name, Long departmentId, Long type,String gpsX, String gpsY, String eventType, Long strength,String category,String mainEquipment,String phone,Boolean isFullTime) {
        try {
            ResTeam resTeam = resTeamService.getResTeamById(id);
            resTeam.setName(name);
            resTeam.setDepartmentId(departmentId);
            resTeam.setType(type);
            System.out.println(new BigDecimal(gpsX));
            resTeam.setGpsX(new BigDecimal(gpsX));
            resTeam.setGpsY(new BigDecimal(gpsY));
            resTeam.setEventType(eventType);
            resTeam.setStrength(strength);
            resTeam.setCategory(category);
            resTeam.setMainEquipment(mainEquipment);
            resTeam.setPhone(phone);
            resTeam.setIsFullTime(isFullTime);
            resTeam.setUpdateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            resTeam.setUpdateTime(DateUtil.dateNow());
            resTeamService.updateResTeamById(resTeam);
            return new ResponseJson(Constant.OK, JSON.toJSON(resTeam));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseJson.UPDATE_ERROR;
        }
    }

    /**
     * 删除
     */
    @RequiresPermissions("res:delete")
    @RequestMapping("deleteResTeam")
    public ResponseJson setDisable(Long id) {
        if (null != resTeamService.getResTeamById(id)) {
            if (resTeamService.deleteResTeamById(id)) {
                return new ResponseJson(Constant.OK, "删除成功");
            } else {
                return new ResponseJson(Constant.FAIL, "删除失败");
            }
        } else {
            return new ResponseJson(Constant.OK, "删除成功");
        }
    }
}
